// 获取文章列表
// 定义参数对象
const params = {
    pagenum: 1,
    pagesize: 2,
    cate_id: '',
    state: ''
}
getList()
// 获取数据
function getList() {
    $.ajax({
        url: '/my/article/list',
        data: params,
        success: function (res) {
            const temp = template('list', res)
            $('tbody').html(temp)
            renderPage(res.total)
        }
    })
}

// 删除
$('body').on('click', '.remove', function () {
    $.ajax({
        url: '/my/article/info?id=' + $(this).attr('data-id'),
        method: 'DELETE',
        success: function (res) {
            if (res.code === 0) {
                getList()
            }
        }
    })
})
// 补零
function addZero(num) {
    return num < 10 ? '0' + num : num
}
// 日期过滤器
template.defaults.imports.dataFormat = function (value) {
    const date = new Date(value),
        year = date.getFullYear(),
        month = date.getMonth() + 1,
        day = date.getDate(),
        hour = date.getHours(),
        min = date.getMinutes(),
        sec = date.getSeconds()
    return `${year}-${addZero(month)}-${addZero(day)} ${addZero(hour)}:${addZero(min)}:${addZero(sec)}`

}

$.ajax({
    url: '/my/cate/list',
    success: function (res) {
        const temp = template('cate-list', res)
        $('.cate-list').html(temp)
        layui.form.render()
    }
})

$('.search-form').on('submit', function (e) {
    e.preventDefault()
    params.cate_id = $('.cate-list').val()
    params.state = $('.state-list').val()
    getList()
})

function renderPage(total) {
    layui.laypage.render({
        elem: 'page-box', // 分页容器的 Id
        count: total, // 总数据条数
        limit: params.pagesize,
        limits: [2, 5, 10, 15, 20],
        curr: params.pagenum,
        layout: ['count', 'limit', 'prev', 'page', 'next', 'skip'],
        jump: function (obj, first) {
            if (first) return
            params.pagenum=obj.curr
            params.pagesize=obj.limit
            getList()
        }
    })
}